﻿using System;
using System.Threading.Tasks;
using ECommon.Wrap;

namespace ECommon
{
    /// <summary>
    /// 
    /// </summary>
    public static partial class ResultTool
    {
        /// <summary>
        /// 安全异步封装结果
        /// </summary>
        /// <typeparam name="T">结果泛型</typeparam>
        /// <param name="process">需要进行的处理</param>
        /// <returns>最后的结果封装</returns>
        public static async Task<ExResult<T>> SafeWrapAsync<T>(Func<ExResult<T>> process)
        {
            return await Task.Run(() =>
            {
                return process();
            }).ConfigureAwait(false);
        }

        /// <summary>
        /// 安全异步封装结果
        /// </summary>
        /// <typeparam name="T">结果泛型</typeparam>
        /// <param name="process">需要进行的处理</param>
        /// <returns>最后的结果封装</returns>
        public static async Task<ExResult<T>> SafeWrapAsync<T>(Func<T> process)
        {
            return await Task.Run(() =>
            {
                return SafeWrap(process);
            }).ConfigureAwait(false);
        }

        /// <summary>
        /// 封装结果
        /// </summary>
        /// <typeparam name="T">结果泛型</typeparam>
        /// <param name="process">需要进行的处理</param>
        /// <returns>最后的结果封装</returns>
        public static async Task<ExResult<T>> WrapAsync<T>(Func<T> process)
        {
            return await Task.Run(() =>
            {
                return Wrap(process);
            }).ConfigureAwait(false);
        }


        /// <summary>
        /// 安全异步封装结果
        /// </summary>
        /// <param name="process">需要进行的处理</param>
        /// <returns>最后的结果封装</returns>
        public static async Task<ExResult> SafeWrapAsync(Action process)
        {
            return await Task.Run(() =>
            {
                return SafeWrap(process);
            }).ConfigureAwait(false);
        }

        /// <summary>
        /// 封装结果
        /// </summary>
        /// <typeparam name="T">结果泛型</typeparam>
        /// <param name="process">需要进行的处理</param>
        /// <returns>最后的结果封装</returns>
        public static async Task<ExResult> WrapAsync(Action process)
        {
            return await Task.Run(() =>
            {
                return Wrap(process);
            }).ConfigureAwait(false);
        }
    }
}
